package cn.edu.ujn.ch12.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.ujn.ch12.model.User;

@Controller
@RequestMapping("/hello")
public class UserController {
	@RequestMapping("/first")
	public String first(HttpServletRequest request , Model model) {
		String id = request.getParameter("id");
		System.out.println(id);
		model.addAttribute("msg", "shaohui~"+id);
		return "first";
	}
	
	@GetMapping("/shaohui")/*只需声明匹配的变量名即可匹配*/
	public String shaohui(int id,Model model) {
		model.addAttribute("msg", "shaohui"+id);
		return "first";
	}
	
	@GetMapping(value="/login")
	public String toLogin(int id,Model model) throws Exception {
		model.addAttribute("msg", "shaohui"+id);		
		return "login";
	}
	
	@RequestMapping(value="/login")
	public String login(User user,HttpServletRequest request,Model model) throws Exception {
		System.out.println(user);		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if("shaohui".equals(username)) {
			model.addAttribute("msg", "欢迎"+username);
			return "ok";
		}else {
			return "login";
		}
		
	}	
	
	@RequestMapping(value="/loginPro")/*Bean化抽象Model&&Servlet为具体*/
	public String loginPro(User user) throws Exception {
		System.out.println(user);		
		if("shaohui".equals(user.getUsername())) {
			return "ok";
		}else {
			return "login";
		}
		
	}	

}
